import store from '../store/store.js'
import config from '../config.js'

import format from '@/common/utils/format.js'
import array from '@/common/utils/array.js'
import img from '@/common/utils/img.js'

import Fingerprint2 from 'fingerprintjs2'

//纪录当前的位置
function SetPosition() {
    let position = window.scrollY //记录离开页面的位置
    if (position == null) position = 0
    store.commit('changeRecruitScrollY', position) //离开路由时把位置存起来
}

function GetUrl(_url) {
    if (_url.length <= 0) {
        return '';
    }
    if (_url.indexOf('http') >= 0) {
        return _url
    }

    //判断最后一个字符是否存在/
    var basepath = config.img_url;
    if (basepath.charAt(basepath.length - 1) === "/") {
        basepath = basepath.substring(0, basepath.length - 1);
    }

    return basepath + _url;
}

function VerifyCode() {
    return config.api_url + '/tools/verify_code.ashx?time=' + new Date();
}
//随机数
function GetRandom(n) {
    var chars = ['2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
    var res = "";
    for (var i = 0; i < n; i++) {
        var id = Math.ceil(Math.random() * 30);
        res += chars[id];
    }
    return res;
}
//返回上一页
function goBack(v, class_layer) {
    try {
        appHost.goback(class_layer) //android
    } catch (e) {
        try {
            window.webkit.messageHandlers.goback.postMessage(class_layer); //ios
        } catch (e) {
            v.$router.go(class_layer);
        }
    }

}
//去除所有空格
function trim(str) {
    return str.replace(/\s+/g, "");
}
//校验手机号码
function checkMobile(mobile) {
    /// <summary>验证手机号码</summary>
    /// <param name="mobile" type="String">需要验证的文字</param>

    var ismobi = /^1[3|4|5|6|7|8|9][0-9]\d{4,8}$/.test(mobile);
    if (ismobi && !isNaN(mobile) && mobile.length == 11) {
        return true;
    }
    return false;
}

function guid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
    s[8] = s[13] = s[18] = s[23] = "-";

    var uuid = s.join("");
    return uuid.replace('-', '');
}

function HideString(_str, _start, _end) {
    if (_str.length < (_start + _end)) {
        return _str;
    }
    var startStr = _str.substring(0, _start);
    var endStr = _str.substring(_str.length - _end);
    var c = _str.length - (_start + _end);
    for (var i = 0; i < c; i++) {
        startStr += '*'
    }
    return startStr + endStr
}

function CutDown(_v, _time, _options) {
    _v[_options.time] = _time;
    _v[_options.t] = setTimeout(function() {
        if (_time == 0) {
            clearTimeout(_v[_options.t])
            typeof _options.end === "function" && _options.end();
            return;
        }
        _time--;
        CutDown(_v, _time, _options);
    }, 1000);
}

function HTMLEncode(html) {

    var temp = document.createElement("div");
    (temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html);
    var output = temp.innerHTML;
    temp = null;
    // var reg = new RegExp('&amp;' , "g" )
    // var newstr = output.replace( reg , '&' );
    return output;
}

function DeleteHtml(_html, _length) {
    _html = _html.replace(/<[^>]+>|&[^>]+;/g, "").trim(); //去掉所有的html标签和&nbsp;之类的特殊符合
    if (_length && _length > 0) {
        if (_html.length <= _length) {
            return _html;
        }
        return _html.substring(0, _length - 1);
    }
    return _html;
}

function GetDateStr(AddDayCount, _init) {
    var dd = new Date();
    dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1; //获取当前月份的日期
    var d = dd.getDate();

    return format.ToTime(y + "-" + m + "-" + d, _init);
}

function GetUrlParam(_name) {
    var reg = new RegExp('(^|&)' + _name + '=([^&]*)(&|$)');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null
}

function isMobile(_val) {
    if (_val.length != 11) {
        return false;
    }
    return true;
}
function GetFingerprint(){
    var murmur=window.localStorage.getItem('murmur');
    if(murmur&&murmur.length>0) {
      return murmur;
    }
    Fingerprint2.get(function(components) {
      const values = components.map(function(component,index) {
        if (index === 0) { //把微信浏览器里UA的wifi或4G等网络替换成空,不然切换网络会ID不一样
          return component.value.replace(/\bNetType\/\w+\b/, '')
        }
        return component.value
      })
      // 生成最终id murmur   
      murmur = Fingerprint2.x64hash128(values.join(''), 31)
      window.localStorage.setItem('murmur',murmur)
      
  
    })
  }

export default {
    guid: guid, //生成guid
    GetUrl: GetUrl, //组合地址
    GetRandom: GetRandom, //获取随机数
    goBack: goBack, //返回上一页
    SetPosition: SetPosition, //纪录当前页面位置
    VerifyCode: VerifyCode, //刷新验证码
    trim: trim, //去除所有空格
    checkMobile: checkMobile, //校验手机号码
    HideString: HideString, //替换字符为*号
    CutDown: CutDown, //倒计时
    HTMLEncode: HTMLEncode, //html转义
    DeleteHtml: DeleteHtml, //过滤html标签
    GetDateStr: GetDateStr, //获取时间
    GetUrlParam: GetUrlParam, //获取请求里面的参数
    isMobile: isMobile, //验证手机号码
    GetFingerprint:GetFingerprint,//获取浏览器指纹
    ...format,//格式化帮助类
    ...array,//数组操作辅助类
    ...img,//图片操作辅助类
}